#include <dspSimulator/instruction/addu.h>


bool ADDU::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    return checkStandardCondition(dspMachine);
}

void ADDU::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                             std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
    llvm::FunctionType *funcType = llvm::FunctionType::get(irBuilder->getVoidTy(), false);
    // ----------------------------------- ADDU (.L1 or .L2) src1, src2, dst_h:dst_l---------------------------------------------
    {
        llvm::Function *addu = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "ADDUKind1",
                                                      module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", addu);
        irBuilder->SetInsertPoint(entryBB);

        auto src1RegPtr = getSrc1LoRegPtr(module, irBuilder);
        auto src1Reg = irBuilder->CreateLoad(src1RegPtr);
        auto src1Reg40 = irBuilder->CreateZExt(src1Reg, irBuilder->getIntNTy(40));

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2Reg = irBuilder->CreateLoad(src2RegPtr);
        auto src2Reg40 = irBuilder->CreateZExt(src2Reg, irBuilder->getIntNTy(40));

        auto res = irBuilder->CreateAdd(src1Reg40, src2Reg40);
        auto res64 = irBuilder->CreateZExt(res, irBuilder->getIntNTy(64));

        auto resLo = irBuilder->CreateTrunc(res64, irBuilder->getInt32Ty());
        auto shr = irBuilder->CreateLShr(res64, irBuilder->getIntN(64, 32));
        auto resHi = irBuilder->CreateTrunc(shr, irBuilder->getInt32Ty());

        auto dstLoRegPtr = getDstLoRegPtr(module, irBuilder);
        auto dstHiRegPtr = getDstHiRegPtr(module, irBuilder);
        irBuilder->CreateStore(resLo, dstLoRegPtr);
        irBuilder->CreateStore(resHi, dstHiRegPtr);
        irBuilder->CreateRetVoid();
    }
}

std::shared_ptr<Instruction> ADDU::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<ADDU> addu;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<10>(data, 2, 0b0101011110) && existStandardCondition(data)) {
            addu = std::make_shared<ADDU>(dspInstInfo->getAddress(), baseCycle);
            addu->creg_z = extractBits<4>(data, 28);
            addu->dst = extractBits<5>(data, 23).to_ulong();
            addu->src2 = extractBits<5>(data, 18).to_ulong();
            addu->src1 = extractBits<5>(data, 13).to_ulong();
            addu->x = data[12];
            addu->s = data[1];
            addu->p = data[0];
            if (addu->s == 0) {
                addu->funcUnit = FuncUnit::L1;
            } else {
                addu->funcUnit = FuncUnit::L2;
            }
            addu->instFormat = InstFormat::Kind1;
        }
    }
    return addu;
}

void ADDU::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {

}

std::string ADDU::toString() const {
    std::string res;
    if (!isCompactInstruction) {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    if (instFormat == InstFormat::Kind1) {
        res += getSrc1Reg32Name() + "," + getSrc2Reg64Name() + "," + getDstReg64Name();
    }
    return res;
}

std::shared_ptr<DspInstruction> ADDU::clone() {
    return std::make_shared<ADDU>(*this);
}
